//
//  XQPrivacyPopView.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/21.
//

#import "XQPrivacyPopView.h"

@interface XQPrivacyPopView()<UIScrollViewDelegate>
@property (nonatomic,strong) UIView *centerView;
@property (nonatomic,strong) UIImageView *imageView;
@property (nonatomic,strong) UILabel *titleLb;
@property (nonatomic,strong) UIButton *closeButton;
@property (nonatomic,strong) UIButton *confimButton;
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) UILabel *contentLb;
@end

@implementation XQPrivacyPopView


- (instancetype)init
{
    self = [super init];
    if (self) {
        [self makeUI];
        [self makeConstraints];
    }
    return self;
}

- (void)makeUI {
    [self addSubview:self.centerView];
    [self.centerView addSubview:self.imageView];
    [self.imageView addSubview:self.titleLb];
    [self.imageView addSubview:self.closeButton];
    [self.centerView addSubview:self.confimButton];
    [self.imageView addSubview:self.scrollView];
    [self.scrollView addSubview:self.contentLb];
}

- (void)makeConstraints {
    [self.centerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
        (void)make.center;
    }];
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0);
        make.left.equalTo(@12);
        make.right.equalTo(@-12);
        make.height.mas_equalTo(479);
    }];
    [self.confimButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imageView.mas_bottom).offset(10);
        make.left.equalTo(@12);
        make.right.equalTo(@-12);
        make.width.mas_equalTo([UIScreen mainScreen].width - 24);
        make.height.mas_equalTo(45);
        make.bottom.equalTo(@0);
    }];
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        (void)make.centerX;
        make.top.equalTo(@12);
    }];
    [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@12);
        make.right.equalTo(@-10);
        make.height.width.mas_equalTo(22);
    }];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@69);
        make.left.equalTo(@20);
        make.right.equalTo(@-20);
        make.bottom.equalTo(@-48);
    }];
    [self.contentLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo([UIScreen mainScreen].width - 64);
        make.edges.insets(UIEdgeInsetsZero);
    }];
}

#pragma mark - private

- (void)confimClick {
    if (self.confimHandle) {
        self.confimHandle();
    }
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self checkIfScrolledToBottom];
}

- (void)checkIfScrolledToBottom {
    CGFloat offsetY = self.scrollView.contentOffset.y;
    CGFloat contentHeight = self.scrollView.contentSize.height;
    CGFloat scrollViewHeight = self.scrollView.frame.size.height;

    if (offsetY + scrollViewHeight >= contentHeight) {
        self.confimButton.enabled = YES;
    }
}

#pragma mark - lazy

- (UIView *)centerView {
    if (!_centerView) {
        _centerView = [[UIView alloc] init];
    }
    return _centerView;
}

- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        _imageView.image = [UIImage imageNamed:@"privacy_pop"];
        [_imageView setUserInteractionEnabled:YES];
    }
    return _imageView;
}

- (UIButton *)confimButton {
    if (!_confimButton) {
        _confimButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _confimButton.enabled = NO;
        [_confimButton setTitle:@"Confirm" forState:UIControlStateNormal];
        [_confimButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _confimButton.titleLabel.font = [UIFont boldSystemFontOfSize:20];
        [_confimButton setBackgroundImage:[UIImage imageWithColor:kBlueTextColor] forState:UIControlStateNormal];
        [_confimButton setBackgroundImage:[UIImage imageWithColor:[kBlueTextColor colorWithAlphaComponent:0.5]] forState:UIControlStateDisabled];
        _confimButton.layer.cornerRadius = 22.5;
        _confimButton.layer.masksToBounds = YES;
        [_confimButton addTarget:self action:@selector(confimClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _confimButton;
}

- (UILabel *)titleLb {
    if (!_titleLb) {
        _titleLb = [[UILabel alloc] init];
        _titleLb.text = @"Loan Agreement";
        _titleLb.textColor = [UIColor whiteColor];
        _titleLb.font = [UIFont boldSystemFontOfSize:18];
    }
    return _titleLb;
}

- (UIButton *)closeButton {
    if (!_closeButton) {
        _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeButton setImage:[UIImage imageNamed:@"pop_close"] forState:UIControlStateNormal];
        [_closeButton addTarget:self action:@selector(confimClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeButton;
}

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.delegate = self;
    }
    return _scrollView;
}

- (UILabel *)contentLb {
    if (!_contentLb) {
        _contentLb = [[UILabel alloc] init];
        _contentLb.numberOfLines = 0;
        _contentLb.textColor = kBlackTextColor;
        _contentLb.font = [UIFont systemFontOfSize:14];
        _contentLb.textAlignment = NSTextAlignmentCenter;
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"JoinPeso App is a professionalpersonal credit loan application thatcan provide users with up to 60000yuan in funding services withsimple information. We haveprovided financial services to overone million users, helping themsolve various financialproblems.Rapid Lend App is a professional personal credit loan JoinPeso App is a professionalpersonal credit loan application thatcan provide users with up to 60000yuan in funding services withsimple information. We haveprovided financial services to overone million users, helping themsolve various financialproblems.Rapid Lend App is a professional personal credit loan JoinPeso App is a professionalpersonal credit loan application thatcan provide users with up to 60000yuan in funding services withsimple information. We haveprovided financial services to overone million users, helping themsolve various financialproblems.Rapid Lend App is a professional personal credit loan ..."];
            
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineSpacing = 10; // 设置行高（行间距）
        
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attributedString.length)];
        
        // 设置 UILabel 的 attributedText
        _contentLb.attributedText = attributedString;
    }
    return _contentLb;
}

@end
